package com.yayo.mangement.facade.impl;

import com.yayo.mangement.converter.SysRoleConverter;
import com.yayo.mangement.facade.SysRoleFacade;
import com.yayo.mangement.model.SysRoleDO;
import com.yayo.mangement.dto.SysRoleDTO;
import com.yayo.mangement.service.SysRoleService;
import com.yayo.mangement.paging.Paging;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.yayo.mangement.response.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * @author: Yayo
 * @date: 2020-08-07 14:10:31
 */
@Service
public class SysRoleFacadeImpl implements SysRoleFacade{

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleConverter sysRoleConverter;

    @Override
    public Response<Paging<SysRoleDTO>> paging(Integer pageNo, Integer pageSize,Map<String,Object> params){
        Paging<SysRoleDO> sysRoleDOPaing = sysRoleService.paging(pageNo,pageSize,params);
        List<SysRoleDTO> sysRoleDTOList = sysRoleDOPaing.getData().stream().map(sysRoleConverter::DO2DTO).collect(Collectors.toList());
        return Response.ok(new Paging<SysRoleDTO>(sysRoleDOPaing.getTotal(),sysRoleDTOList));
    }

    @Override
    public Response<SysRoleDTO> detail(Long id){
        SysRoleDO sysRoleDO = sysRoleService.findById(id);
        return Response.ok(sysRoleConverter.DO2DTO(sysRoleDO));
    }

    @Override
    public Response create(SysRoleDTO sysRoleDTO){
        SysRoleDO sysRoleDO = sysRoleConverter.DTO2DO(sysRoleDTO);
        Boolean isTrue = sysRoleService.create(sysRoleDO);
        if(isTrue){
            return Response.ok();
        }
        return Response.fail("create.data.error");
    }

    @Override
    public Response update(SysRoleDTO sysRoleDTO){
        SysRoleDO sysRoleDO = sysRoleConverter.DTO2DO(sysRoleDTO);
        Boolean isTrue = sysRoleService.updateById(sysRoleDO);
        if(isTrue){
            return Response.ok();
        }
        return Response.fail("update.date.error");
    }

    @Override
    public Response delete(Long id){
        boolean isTrue = sysRoleService.delete(id);
        if(isTrue){
            return Response.ok();
        }
        return Response.fail("delete.date.error");
    }

}